package org.firefly.fx.service.common.impl;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.firefly.fx.service.common.ICacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

@Service
public class CacheServiceImpl implements ICacheService {
	@Autowired private RedisTemplate<Serializable, Serializable> redis;
	@Override
	public boolean set(String key, String value) {//感觉还不如memcache简洁
		final String ckey = key;
		final String cvalue = value;
		return redis.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String>  ser = redis.getStringSerializer();
				connection.set(ser.serialize(ckey), ser.serialize(cvalue));
				return true;
			}
		});
	}

	@Override
	public boolean set(String key, String value, long expireTime) {
		final String ckey = key;
		final String cvalue = value;
		final long cexpire = expireTime;
		return redis.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String>  ser = redis.getStringSerializer();
				connection.set(ser.serialize(ckey), ser.serialize(cvalue));
				return connection.expire(ser.serialize(ckey), cexpire);
			}
		});
	}

	@Override
	public boolean refresh(String key, long expireTime) {
		final String ckey = key;
		final long cexpire = expireTime;
		return redis.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String>  ser = redis.getStringSerializer();
				return connection.expire(ser.serialize(ckey), cexpire);
			}
		});
	}

	@Override
	public String get(String key) {
		if(StringUtils.isBlank(key)){
			return "";
		}
		
		final String ckey = key;
		return redis.execute(new RedisCallback<String>() {
			@Override
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String>  ser = redis.getStringSerializer();
				byte[] ckeybyte = ser.serialize(ckey);
				if (connection.exists(ckeybyte)) {  
					byte[] value = connection.get(ckeybyte);
		            return new String(value);    
		        } 
				return "";
			}
		});
	}

	@Override
	public boolean del(String key) {
		final String ckey = key;
		return redis.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String>  ser = redis.getStringSerializer();
				byte[] ckeybyte = ser.serialize(ckey);
				connection.del(ckeybyte);
				return true;
			}
		});
	}

}
